import request from 'utils/request'
import { GET_CHANNEL_LIST,GET_ARTICLE_LIST } from '../constants/actionType'
export const getChannelList = () => {
  return async (dispatch) => {
    const res = await request.get('/channels')
  
    dispatch({
      type: GET_CHANNEL_LIST,
      payload: res.data.channels
    })
  }
}


export const getArticleList = (params) => {
    return async (dispatch) => {
      const res = await request({
        url: '/mp/articles',
        params
      })
      dispatch({
        type: GET_ARTICLE_LIST,
        payload: res.data
      })
    }
  }

  export const delArticle = (id) => {
    return async () => {
      await request({
        method: 'delete',
        url: `/mp/articles/${id}`
      })
    }
  }

      // 删除后不需要操作reducer，只需在组件调用 dispatch 提交actions时再次重新请求文章列表即可（getArticlesData）
    // 因此此处删除文章时也就无需去store定义actionType以及reduce



    //发布文章
    export const saveArticle = (data,draft=false) => {

      return async () => {
        await request({
          url: '/mp/articles',
          method: 'post',
          params: { draft },
          data,
        })
      }
    }


//获取文章详情
    export const getArticleInfo = (id) => {
      return async (dispatch) => {
        const res = await request(`/mp/articles/${id}`)
        return res.data
      }
    }

//编辑
    export const editArticle = (draft = false, data) => {
      return async (dispatch) => {
        await request({
          url: `/mp/articles/${data.id}`,
          method: 'put',
          data,
          params: {
            draft
          }
        })
      }
    }